package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.common.pojo.Employee;
import com.itheima.common.result.PageInfo;
import com.itheima.common.vo.EmployeeVO;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author liguoping
 */
@Service
@Slf4j

public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public EmployeeVO login(String username, String password) {
        String passwordMd5 = SecureUtil.md5(password);
        log.info("{}", passwordMd5);
        Employee employee = employeeMapper.selectEmployeeByUsernameAndPassword(username, passwordMd5);
        //找不到员工
        log.info("{}", employee);
        if (ObjectUtil.isEmpty(employee)) {
            return null;
        }
        //生成token
        String token = System.currentTimeMillis() + employee.getId();
        //插入redis库
        redisTemplate.opsForValue().set("employee_token:"+token, JSON.toJSONString(employee),30, TimeUnit.MINUTES);
//        employeeMapper.updateToken(token,employee.getId());
        EmployeeVO employeeVo = new EmployeeVO();
        BeanUtils.copyProperties(employee, employeeVo);
        log.info("{}", employeeVo);
        //token 唯一，安全
        employeeVo.setToken(token);
        return employeeVo;
    }

    @Override
    public void clearToken(String token) {

        Employee employee = employeeMapper.getByToken(token);
        employeeMapper.clearToken(employee.getId());
    }

    @Override
    public EmployeeVO getById(String id) {
        Employee employee = employeeMapper.getById(id);
        if (employee == null) {
            log.info("没有找到该用户");
            return null;
        }
//        if (employee.getStatus() != 1) {
//            log.info("该用户已被删除");
//            return null;
//        }
        EmployeeVO employeeVO = new EmployeeVO();
        BeanUtils.copyProperties(employee, employeeVO);
        return employeeVO;
    }

    @Override
    public boolean save(Employee employee) {
        if (ObjectUtil.isEmpty(employee)) {
            log.info("传进来的对象为空{}", employee);
            return false;
        }
        //自动生成雪花id
        String id = IdUtil.getSnowflakeNextIdStr();
        //在设置进pojo类
        employee.setId(id);
        //设置初始密码
        String password = SecureUtil.md5("123456");
        employee.setPassword(password);
        int res = employeeMapper.save(employee);
        return res > 0;
    }

    @Override
    public PageInfo<Employee> page(String name, Integer page, Integer pageSize) {
        PageInfo<Employee> pageInfo = new PageInfo<Employee>();
        Integer start = (page - 1) * pageSize;
        List<Employee> employees = employeeMapper.page(name, start, pageSize);
        pageInfo.setRecords(employees);
        pageInfo.setCurrent(page);
        long count = employeeMapper.countEmployee(name);
        pageInfo.setTotal(count);
        return pageInfo;
    }

    @Override
    public boolean updateEmployee(Employee employee) {
//        EmployeeVO employeeDb = getById(employee.getId());
//        if (employeeDb == null) {
//            log.info("无该对象，不能修改");
//            return false;
//        }
//        if (StrUtil.isBlank(employee.getId())) {
//            log.info("id不可为空，不然会导致全部用户被修改{}", employee.getId());
//            return false;
//        }
//        log.info("{}", employee);
//        employee.setUpdateTime(LocalDateTime.now());
        int res = employeeMapper.updateEmployee(employee);
        return res > 0;
    }
}
